<template>
  <div class="list-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          添加
        </el-button>
        <!-- <el-button icon="el-icon-delete" type="danger" @click="handleDelete">
          删除
        </el-button> -->
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-form
          ref="form"
          :model="queryForm.queryBean"
          :inline="true"
          @submit.native.prevent
        >
          <el-form-item>
            <el-input
              v-model.trim="queryForm.queryBean.phone"
              placeholder="手机号码"
              clearable
            />
          </el-form-item>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.queryBean.userName"
              placeholder="用户名"
              clearable
            />
          </el-form-item>
          <el-form-item>
            <el-button
              icon="el-icon-search"
              type="primary"
              native-type="submit"
              @click="handleQuery"
            >
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>
    <el-table
      ref="tableSort"
      v-loading="listLoading"
      :data="list"
      :element-loading-text="elementLoadingText"
      :height="height"
    >
      <el-table-column
        show-overflow-tooltip
        prop="userName"
        label="用户名称"
        width="100"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="手机号码"
        prop="phone"
      ></el-table-column>
      <!-- <el-table-column
        show-overflow-tooltip
        label="用户ID"
        prop="userId"
      ></el-table-column> -->
      <el-table-column show-overflow-tooltip label="操作" width="200">
        <template #default="{ row }">
          <el-button
            type="primary"
            icon="el-icon-edit"
            @click="handleEdit(row)"
          ></el-button>
          <el-button
            type="primary"
            icon="el-icon-set-up"
            @click="handleSet(row)"
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(row)"
          ></el-button>
          <!-- <el-button type="text" >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <resident-form ref="edit" @fetch-data="fetchData"></resident-form>
    <set-tree ref="set" @fetch-data="fetchData"></set-tree>
    <el-pagination
      :background="background"
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
  </div>
</template>
<script>
  import { getUserList, deleteUser } from '@/api/user'
  import ResidentForm from './components/ResidentForm'
  import SetTree from './components/SetTree.vue'
  export default {
    name: 'List',
    components: { ResidentForm, SetTree },
    data() {
      return {
        imgShow: true,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        background: true,
        selectRows: '',
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNum: 1,
          pageSize: 10,
          queryBean: {
            roleId: '4',
          },
        },
      }
    },
    computed: {
      height() {
        return this.$baseTableHeight()
      },
    },
    created() {
      this.fetchData()
    },
    mounted() {},
    methods: {
      handleEdit(row) {
        if (row.roleId) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleSet(row) {
        if (row.roleId) {
          this.$refs['set'].showEdit(row)
        } else {
          this.$refs['set'].showEdit()
        }
      },
      handleDelete(row) {
        if (row.userId) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { data } = await deleteUser({ userId: row.userId })
            if (data.status == 0) {
              this.$baseMessage('删除成功！', 'success')
            } else {
              this.$baseMessage(data.message, 'console.error();')
            }
            this.fetchData()
          })
        } else {
          return false
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      handleQuery() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        console.log(this.queryForm)
        const { data } = await getUserList(this.queryForm)
        const { body } = data
        console.log(body.data)
        this.list = body.data
        // data.forEach((item, index) => {})
        this.total = body.totalSize
        this.$baseNotify(
          '驻场人员数据获取完毕！',
          '成功',
          'success',
          'top-right'
        )
        setTimeout(() => {
          this.listLoading = false
        }, 500)
      },
    },
  }
</script>
